<?php
include_once 'protocol_includes.php';

class CreateSoftwareDescription extends subapplication
{
    private $cmd;
    public static $PROTOCOL = "CreateSoftwareDescription";

    public static function create(command $cmd)
    {
        if (!self::IsThisClass($cmd)) return null;
        $N = new CreateSoftwareDescription($cmd);
        return $N;
    }

    public function __construct(command $cmd)
    {
        $this->cmd  = $cmd;
        $this->LoginKey($cmd->GetData('LOGINKEY'));
        $this->Name($cmd->GetData('NAME'));
        $this->Description($cmd->GetData('DESCRIPTION'));
        $this->Category($cmd->GetData('CATEGORY'));        
    }

    public function  requiresAuthentication() {
        return true;
    }

    public function execute()
    {
        $returnString = "NS_PROTOCOL=SoftwareDescriptionCreation&";
        $confirmationString = "NS_COMFIRMATION=false";

        $username = ns_dao_user::GetUsernameFromLoginKey($this->LoginKey());
        $success = ns_dao_software::CreateNewSoftwareDescription($this->Name(),$this->Description(), $this->Category(),$username, 0);
        if ($success > 0) {
            $confirmationString = "NS_COMFIRMATION=true";
        }
        $returnString = $returnString . $confirmationString;
        
        $this->UpdateResult($returnString);
    }


    // =========== PROPERTIES ==========

    private $property = array();
    private function GetSetData($property_name,$value = null)
    {
        if (is_null($value) && isset($this->property[$property_name])) return $this->property[$property_name];
        $this->property[$property_name] = $value;
    }

    public function LoginKey($value = null)
    {
        return $this->GetSetData("LOGINKEY",$value);
    }

    public function Name($value = null)
    {
        return $this->GetSetData("NAME",$value);
    }
    
    public function Description($value = null)
    {
        return $this->GetSetData("DESCRIPTION",$value);
    }
    
    public function Category($value = null)
    {
        return $this->GetSetData("CATEGORY",$value);
    }

    public function result()
    {
        $this->cmd->GetResult();
    }

    public function UpdateResult($str)
    {
        $this->cmd->SetResult($str);
    }

    public static function IsThisClass(command $cmd)
    {
        if (!$cmd->HasDataKey('LOGINKEY')) return false;
        if (!$cmd->HasDataKey('NAME')) return false;
        if (!$cmd->HasDataKey('DESCRIPTION')) return false;
        if (!$cmd->HasDataKey('CATEGORY')) return false;
        return true;
    }

}

?>
